﻿using DG.Tweening;
using HBFramework;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class Panel_TextTip : BasePanel
{
    public override int RemainTime => PanelRemainTime.Permanent;

    public override int Layer => PanelLayer.Tip;

    public override string Path => "ui/panel_texttip";

    private CanvasGroup img_bg;
    private TMP_Text tmp_content;

    private static Queue<string> tipQueue;
    private bool isShowing = false;

    public override void OnInit(GameObject ui)
    {
        img_bg = ui.transform.Find("img_bg").GetComponent<CanvasGroup>();
        tmp_content = ui.transform.Find("img_bg/tmp_content").GetComponent<TMP_Text>();
        img_bg.alpha = 0;

        tipQueue = new Queue<string>();
    }

    public override void OnRelease()
    {

    }

    public override void OnUpdate()
    {
        if (tipQueue != null
            && tipQueue.Count > 0
            && !isShowing)
        {
            tmp_content.text = tipQueue.Peek();
            var sq = DOTween.Sequence();
            sq.Append(img_bg.DOFade(1, .3f));
            sq.Append(img_bg.DOFade(0, 0.3f).SetDelay(2));
            sq.OnComplete(() =>
            {
                tipQueue.Dequeue();
                isShowing = false;
            });
            sq.Play();
            isShowing = true;
        }
    }

    /// <summary>
    /// 显示提示
    /// </summary>
    /// <param name="tip"></param>
    public static void ShowTip(string tip)
    {
        if (tipQueue.Count > 0
            && tipQueue.Peek() == tip)
            return;
        tipQueue.Enqueue(tip);
    }
}
